/*
 * @Author: 码上talk|RC
 * @Date: 2021-05-05 09:21:08
 * @LastEditTime: 2022-04-14 13:32:56
 * @LastEditors: 码上talk|RC
 * @Description:
 * @FilePath: /ma-distributor/model/entity/goods/index.js
 * @微信:  13680065830
 * @邮箱:  3189482282@qq.com
 * @oops: Just do what I think it is right
 */
import _ from 'lodash'
import Entity from '../../entity.class.js';

const _tableField = {
  id: {
    type: 'int',
    default: 0
  },
  type: {
    type: 'int',
    default: 1
  },
  sn: {
    type: 'string',
    default: ''
  },
  name: {
    type: 'string',
    default: ''
  },
  description:{
    type: 'string',
    default: ''
  },
  shopStock: {
    type: 'int',
    default: 0
  },
  thumbnail: {
    type: 'string',
    default: ''
  },
  amount: {
    type: 'int',
    default: 0
  },
  amountCost: {
    type: 'int',
    default: 0
  },
  integralShopGive: {
    type: 'int',
    default: 0
  },
  integralExchangeRequire: {
    type: 'int',
    default: 0
  },
  integralExchangeGive: {
    type: 'int',
    default: 0
  },
  detailMobileText: {
    type: 'string',
    default: ''
  },
  createTime: {
    type: 'string',
    default: ''
  },
  gallery: {
    type: 'string',
    do: {
      parse: true
    },
    default: ''
  },
  goodsSpecCategory :{
    type: 'Object',
    default: {}
  },
  goodsItemsList:{
    type: 'Array',
    default: []
  }
};
class Goods extends Entity {
  static _requestConfig = {
    app: 'portal',
    domain: 'goods'
  }

  static _form = {}

  static options = {}

  constructor (goods) {
    super(goods, { _tableField });
  }

  _getSpec (key, opt) {
    if (_.get(this, '_raw.goodsSpecCategory.goodsSpecKeyList', []).length === 0) {
      return [];
    }
    const validSpecList = _.get(this, '_raw.goodsItemsList', []).map(i => _.get(i, 'specJson', '[]')).map(i => JSON.parse(i || '[]')).reduce((p, c) => {
      return p.concat(c);
    }, []);
    return _.get(_.find(_.get(this, '_raw.goodsSpecCategory.goodsSpecKeyList'), i => { return i.name === key }), 'goodsSpecValueList').map(i => ({ value: i.id, label: i.name, _raw: i })).filter(i => {
      const { skipNotExist } = opt;
      if (skipNotExist) {
        return validSpecList.map(i => i.value_id).includes(i.value)
      }
      return true;
    });
  }

  getSpecSize (opt = {}) {
    return this._getSpec('尺码', opt);
  }

  getSpecColor (opt = {}) {
    return this._getSpec('颜色', opt);
  }

  getGoodsItemBySpc (a) {
    return _.find(_.get(this, 'goodsItemsList', []), i => {
      if (!i.specJson) {
        return false;
      }
      return _.sortBy(a).join('-') === _.sortBy(i.specJson.map(i => i.value_id)).join('-')
    })
  }

}

export default Goods;
